package com.itheima.service.front.impl;

import com.itheima.dao.front.MemberDao;
import com.itheima.domain.front.Member;
import com.itheima.factory.MapperFactory;
import com.itheima.service.front.MemberService;
import com.itheima.utils.JedisUtils;
import com.itheima.utils.MD5Util;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.UUID;

/**
 * @program: mm_exam
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-18 20:54
 **/
public class MemberServiceImpl implements MemberService {
    @Override
    public boolean register(Member member) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            MemberDao memberDao = MapperFactory.getMapper(sqlSession, MemberDao.class);

            //
            String id = UUID.randomUUID().toString();
            member.setId(id);

            member.setRegisterDate(new Date());
            member.setPassword(MD5Util.md5(member.getPassword()));
            member.setState("1");
            //3.调用Dao层操作
            int row = memberDao.save(member);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
            return row > 0;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Member login(String email, String password) {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            MemberDao memberDao = MapperFactory.getMapper(sqlSession, MemberDao.class);

            //
            password = MD5Util.md5(password);
            Member member = memberDao.findByEmailAndPassword(email, password);
            //登录人的信息保存到redis中
            Jedis jedis = JedisUtils.getResource();
            //使用登录人的id作为key,设置3600秒为过期时间,value值待定
            jedis.setex(member.getId(), 3600, member.getNickName());
            jedis.close();

            return member;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getLoginInfo(String id) {
        Jedis jedis = JedisUtils.getResource();
        String nickName = jedis.get(id);
        jedis.close();
        return nickName;
    }

    @Override
    public boolean logout(String id) {
        Jedis jedis = JedisUtils.getResource();
        Long row = jedis.del(id);
        return row > 0;
    }
}
